package cn._51doit.flink.day02.sources;

import org.apache.flink.api.common.io.FileInputFormat;
import org.apache.flink.api.java.io.TextInputFormat;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.FileProcessingMode;

/**
 *
 * readFile可以指定多种模式：
 * 默认是FileProcessingMode.PROCESS_ONCE，即数据读完就退出（有限数据流）
 *
 */
public class FileSource2 {

    public static void main(String[] args) throws Exception {

        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port", 8081);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        String filePath = "/Users/start/Desktop/words.txt";
        Path path = new Path(filePath);
        TextInputFormat textInputFormat = new TextInputFormat(path);

        //DataStreamSource<String> lines = env.readFile(textInputFormat, filePath);
        DataStreamSource<String> lines = env.readFile(textInputFormat, filePath, FileProcessingMode.PROCESS_CONTINUOUSLY, 1000);

        //readTextFile方法创建的DataStream的并行度
        System.out.println("readTextFile方法创建的DataStream的并行度: " +lines.getParallelism());
        lines.print();
        env.execute();

    }
}
